﻿using GalaSoft.MvvmLight.CommandWpf;
using GEPL.InterfaceLibrary;
using GEPL.Projects;
using GongSolutions.Wpf.DragDrop;
using PropertyChanged;
using System.Windows;
using System.Windows.Input;

namespace GEPL.ViewModels
{
    /// <summary>
    /// Implements IDropTarget - interface used for drag and drop mechanism.
    /// </summary>
    [ImplementPropertyChanged]
    public class WorkspaceViewModel : IDropTarget
    {
        /// <summary>
        /// Current project.
        /// </summary>
        public Project Project { get; set; }

        /// <summary>
        /// The element currently selected in workspace control
        /// </summary>
        public Element SelectedElement { get; set; }

        public ICommand RemoveElementCommand { get; set; }

        public WorkspaceViewModel()
        {
            InitializeCommands();
        }

        private void InitializeCommands()
        {
            RemoveElementCommand = new RelayCommand(RemoveElement, () => SelectedElement != null);
        }

        /// <summary>
        /// Removes selected element from list
        /// </summary>
        private void RemoveElement()
        {
            if (SelectedElement == null)
                return;
            Project.Elements.Remove(SelectedElement);
        }

        #region IDropTarget members

        /// <summary>
        /// Custom DropHandler for correct drag and drop implementation.
        /// Sets proper effects and adorner.
        /// </summary>
        public void DragOver(IDropInfo dropInfo)
        {
            dropInfo.DropTargetAdorner = DropTargetAdorners.Insert;
            dropInfo.Effects = dropInfo.Data is IElement ? DragDropEffects.Copy : DragDropEffects.Move;
        }

        /// <summary>
        /// Custom DropHandler for correct drag and drop implementation.
        /// Decides whether to move or copy dropped element.
        /// </summary>
        public void Drop(IDropInfo dropInfo)
        {
            var elementFromToolbox = dropInfo.Data as IElement;
            var elementFromWorkspace = dropInfo.Data as Element;

            if (elementFromToolbox != null)
            {
                if (dropInfo.InsertIndex > Project.Elements.Count)
                    return;
                var element = new Element(elementFromToolbox);
                Project.Elements.Insert(dropInfo.InsertIndex, element);
            }
            else if (elementFromWorkspace != null && Project.Elements.Contains(elementFromWorkspace))
            {
                var sourceIndex = Project.Elements.IndexOf(elementFromWorkspace);
                var insertIndex = dropInfo.InsertIndex > 0 && dropInfo.InsertIndex > sourceIndex
                    ? dropInfo.InsertIndex - 1
                    : dropInfo.InsertIndex;
                Project.Elements.Move(sourceIndex, insertIndex);
            }
        }

        #endregion IDropTarget members
    }
}